//===============================================================================
//
// AgileFileInfoCollection
//
// PURPOSE: 
// 
//
// NOTES: 
// 
//
//===============================================================================
//
// Copyright (C) 2003 Wallis Software Solutions
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//
//===============================================================================

namespace Agile.Common
{
    /// <summary>
    /// AgileFileInfoCollection
    /// </summary>
    public class AgileFileInfoCollection : AgileCollection
    {
        /// <summary>
        /// Returns true if there is a file with the given name in the collecion. 
        /// Including the extension but not including the path.
        /// </summary>
        /// <param name="fileName">Name of the file to look for.</param>
        /// <returns></returns>
        public bool ContainsFileNamed(string fileName)
        {
            foreach (AgileFileInfo file in this)
            {
                if (file.Name.Equals(fileName))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Add a collection of AgileFileInfo items to the collection
        /// </summary>
        /// <param name="files">Collection of item to add to the collection.</param>
        public void AddRange(AgileFileInfoCollection files)
        {
            foreach (AgileFileInfo file in files)
                List.Add(file);
        }

        #region Constructors and Factories

        /// <summary>
        /// Constructor
        /// </summary>
        internal AgileFileInfoCollection()
        {
        }

        /// <summary>
        /// Instantiate a new AgileFileInfoCollection
        /// </summary>
        public static AgileFileInfoCollection Build()
        {
            return new AgileFileInfoCollection();
        }

        #endregion

        #region Strongly Typed IList Implementations

        /// <summary>
        /// Returns the AgileFileInfo that is at the given index in the collection.
        /// </summary>
        public AgileFileInfo this[int index]
        {
            get { return InnerList[index] as AgileFileInfo; }
        }

        /// <summary>
        /// Add a new AgileFileInfo to the collection
        /// </summary>
        /// <param name="agileFileInfo">agileFileInfo to add to the collection.</param>
        public void Add(AgileFileInfo agileFileInfo)
        {
            if (Contains(agileFileInfo))
            {
                string message = string.Format("Tried adding a AgileFileInfo to the collection but it is already in it.");
                throw new AgileCommonException(message);
            }
            List.Add(agileFileInfo);
        }

        /// <summary>
        /// Remove a AgileFileInfo from the collection
        /// </summary>
        /// <param name="agileFileInfo">agileFileInfo to remove from the collection.</param>
        public void Remove(AgileFileInfo agileFileInfo)
        {
            InnerList.Remove(agileFileInfo);
        }

        /// <summary>
        /// Checks if the given AgileFileInfo is in the collection
        /// </summary>
        /// <param name="agileFileInfo">agileFileInfo to look for in the collection.</param>
        /// <returns>True if the item is in the collection.</returns>
        public bool Contains(AgileFileInfo agileFileInfo)
        {
            return InnerList.Contains(agileFileInfo);
        }

        /// <summary>
        /// Finds and returns the file with the given name from the collection.
        /// </summary>
        /// <param name="fileName">Name of the file including the extension</param>
        /// <returns>null if the file is not found.</returns>
        public AgileFileInfo Find(string fileName)
        {
            foreach (AgileFileInfo file in this)
            {
                if (file.Name == fileName)
                    return file;
            }
            return null;
        }

        /// <summary>
        /// Searches for the specified AgileFileInfo and returns the zero-based index
        /// of the first occurrence within the section of the
        /// collection that starts at the
        /// specified index and contains the specified number of elements.
        /// </summary>
        /// <param name="agileFileInfo">agileFileInfo to get the index of from the collection.</param>
        /// <returns>The index of the item in the collection.</returns>
        public int IndexOf(AgileFileInfo agileFileInfo)
        {
            return base.InnerList.IndexOf(agileFileInfo);
        }

        /// <summary>
        /// Searches for the specified AgileFileInfo and returns the zero-based index
        /// of the first occurrence within the section of the
        /// collection that starts at the
        /// specified index and contains the specified number of elements.
        /// </summary>
        /// <param name="agileFileInfo">agileFileInfo to get the index of from the collection.</param>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <returns>The index of the item in the collection.</returns>
        public int IndexOf(AgileFileInfo agileFileInfo, int startIndex)
        {
            return base.InnerList.IndexOf(agileFileInfo, startIndex);
        }

        /// <summary>
        /// Searches for the specified AgileFileInfo and returns the zero-based index
        /// of the first occurrence within the section of the
        /// collection that starts at the
        /// specified index and contains the specified number of elements.
        /// </summary>
        /// <param name="agileFileInfo">agileFileInfo to get the index of from the collection.</param>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <param name="count">The number of elements in the section to search.</param>
        /// <returns>The index of the item in the collection.</returns>
        public int IndexOf(AgileFileInfo agileFileInfo, int startIndex, int count)
        {
            return base.InnerList.IndexOf(agileFileInfo, startIndex, count);
        }

        /// <summary>
        /// Inserts the AgileFileInfo into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which value should be inserted.</param>
        /// <param name="agileFileInfo">agileFileInfo to insert into the collection.</param>
        public void Insert(int index, AgileFileInfo agileFileInfo)
        {
            base.InnerList.Insert(index, agileFileInfo);
        }

        /// <summary>
        /// Copies a range of elements from the collection of AgileFileInfo's to a compatible
        /// one-dimensional System.Array, starting at the specified index of the target array.
        /// </summary>
        /// <param name="agileFileInfos">The one-dimensional System.Array that is the destination of the elements copied from the collection. The System.Array must have zero-based indexing.</param>
        /// <param name="index">The zero-based index at which copying begins.</param>
        public void CopyTo(AgileFileInfo[] agileFileInfos, int index)
        {
            List.CopyTo(agileFileInfos, index);
        }

        #endregion
    }
}